package instant.openvfs.home.web;

import android.net.Uri;

public abstract class FileMatrixCursor extends MatrixCursor {

	protected Uri uri = null;
	protected String[] projection = null;
	protected String selection = null;
	protected String[] selectionArgs;
	protected String sortOrder = null;

	public FileMatrixCursor(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		super(BaseFileContentProvider.PROJECTION);
		//
		this.uri = uri;
		this.projection = projection;
		this.selection = selection;
		this.selectionArgs = selectionArgs;
		this.sortOrder = sortOrder;
	}

	protected void load() throws Exception {
		rowCount = 0;
		//
		int size = getRowCount();
		if (size == -1) {
			return;
		}
		//
		data = new Object[size * columnCount];
		for (int i = 0; i < size; i++) {
			Object[] row = mapRow(i);
			addRow(row);
		}
	}

	@Override
	public boolean requery() {
		try {
			load();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * @param row
	 *            the current row index
	 * @return array of objects
	 */
	abstract protected Object[] mapRow(int row);

	/**
	 * This method is called once before mapping the row
	 * 
	 * @param uri
	 *            request uri
	 * @return -1 if not a directory, otherwise the number of items in the
	 *         directory, 0 if empty
	 */
	abstract protected int getRowCount();

}
